package com.map.application.headquarter;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

import java.awt.Frame;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Font;
import java.awt.Toolkit;

import java.awt.Insets;

import javax.swing.JSplitPane;
import javax.swing.ImageIcon;
import javax.swing.JTable;

import com.map.database.Database;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
import java.awt.GridLayout;

public class HeadQuarterMainWindow {

	private JFrame headQuarterFrame;
	private static JTable table;
	private static Database db = new Database();
	// Header Label
	private JLabel headerLabel;
	// Side Item List
	private JButton productButton;
	private JButton vendorsButton;
	private JButton purchaseOrderButton;
	private JButton receivingButton;
	private JButton paymentToVendorsButton;
	private JButton customersButton;
	private JButton salesOrderButton;
	private JButton invoiceButton;
	private JButton customersPaymentsButton;

	/**
	 * Create the application.
	 */
	public HeadQuarterMainWindow() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 * 
	 * @throws ClassNotFoundException
	 */
	private void initialize() {
		headQuarterFrame = new JFrame();
		headQuarterFrame.setTitle("Head Quarter Main Window");
		headQuarterFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(HeadQuarterMainWindow.class.getResource("/resources/earth_icon.png")));
		headQuarterFrame.setBounds(100, 100, 724, 386);
		headQuarterFrame.setLocationRelativeTo(null);
		headQuarterFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
		headQuarterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JMenuBar menuBar = new JMenuBar();
		headQuarterFrame.setJMenuBar(menuBar);

		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);

		JMenuItem mntmItem = new JMenuItem("Item1");
		mnFile.add(mntmItem);

		JMenuItem mntmItem_1 = new JMenuItem("Item2");
		mnFile.add(mntmItem_1);

		JMenuItem mntmItem_2 = new JMenuItem("Item3");
		mnFile.add(mntmItem_2);

		JMenu mnEdit = new JMenu("Edit");
		menuBar.add(mnEdit);

		JMenuItem mntmSome = new JMenuItem("some1");
		mnEdit.add(mntmSome);

		JMenuItem mntmSome_1 = new JMenuItem("some2");
		mnEdit.add(mntmSome_1);

		JMenuItem mntmSome_2 = new JMenuItem("some3");
		mnEdit.add(mntmSome_2);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 0, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
		headQuarterFrame.getContentPane().setLayout(gridBagLayout);

		headerLabel = new JLabel("Product");
		headerLabel.setFont(new Font("Tahoma", Font.PLAIN, 20));
		GridBagConstraints gbc_lblProduct = new GridBagConstraints();
		gbc_lblProduct.insets = new Insets(0, 0, 5, 0);
		gbc_lblProduct.gridx = 0;
		gbc_lblProduct.gridy = 0;
		headQuarterFrame.getContentPane().add(headerLabel, gbc_lblProduct);

		JSplitPane splitPane = new JSplitPane();
		GridBagConstraints gbc_splitPane = new GridBagConstraints();
		gbc_splitPane.fill = GridBagConstraints.BOTH;
		gbc_splitPane.gridx = 0;
		gbc_splitPane.gridy = 1;
		headQuarterFrame.getContentPane().add(splitPane, gbc_splitPane);

		JSplitPane splitPane_1 = new JSplitPane();
		splitPane_1.setOrientation(JSplitPane.VERTICAL_SPLIT);
		splitPane.setRightComponent(splitPane_1);

		
		try {
			db.ConnectApplicationDB();//connect database
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		table = new JTable();
		table.setModel(db.readData("map"));
		db.closeDB();//close database

		JScrollPane scrollpane = new JScrollPane(table);
		splitPane_1.setRightComponent(scrollpane);

		JPanel headerButtonPanel = new JPanel();
		splitPane_1.setLeftComponent(headerButtonPanel);
		headerButtonPanel.setLayout(new GridLayout(1, 5, 0, 0));
		
		//Add button
		JButton addButton = new JButton("");
		addButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AddMap frame = new AddMap();
				frame.setVisible(true);
			}
		});
		addButton.setIcon(new ImageIcon(HeadQuarterMainWindow.class.getResource("/resources/add.png")));
		headerButtonPanel.add(addButton);
				
		//Edit Button
		JButton editButton = new JButton("");
		editButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					EditMap frame = new EditMap(table.getValueAt(table.getSelectedRow(), 1).toString(), table.getValueAt(table.getSelectedRow(), 2).toString(), table.getValueAt(table.getSelectedRow(), 3).toString(), table.getValueAt(
							table.getSelectedRow(), 4).toString(), table.getValueAt(table.getSelectedRow(), 5).toString());
					frame.setVisible(true);
				} catch (ArrayIndexOutOfBoundsException e1) {
					JOptionPane.showMessageDialog(null, "Select Row to Edit !!", "alert", JOptionPane.INFORMATION_MESSAGE);
					e1.printStackTrace();
				}
			}
		});
		editButton.setIcon(new ImageIcon(HeadQuarterMainWindow.class.getResource("/resources/edit.png")));
		headerButtonPanel.add(editButton);

		//Delete Button
		JButton deleteButton = new JButton("");
		deleteButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					Database db = new Database();
					db.ConnectApplicationDB();					
					String updateQuery = "delete from map where mapName = '"+table.getValueAt(table.getSelectedRow(), 1).toString()+"' and mapType='"+table.getValueAt(table.getSelectedRow(), 2).toString()+"' and costPrice = "+table.getValueAt(table.getSelectedRow(), 3).toString()+" and sellingPrice = "+table.getValueAt(table.getSelectedRow(), 4).toString()+" and quantity = "+table.getValueAt(table.getSelectedRow(), 5).toString();
					db.exceuteQueryStatement(updateQuery);
					db.closeDB();
					HeadQuarterMainWindow.updateMapTable();
					JOptionPane.showMessageDialog(null, "Deletion Successfull !!", "alert", JOptionPane.INFORMATION_MESSAGE);					
				} catch (ArrayIndexOutOfBoundsException e1) {
					JOptionPane.showMessageDialog(null, "Select Row to Delete !!", "alert", JOptionPane.INFORMATION_MESSAGE);
					e1.printStackTrace();
				}
				catch (Exception e2) {
					JOptionPane.showMessageDialog(null, "Deletion Failed !!", "alert", JOptionPane.INFORMATION_MESSAGE);
					e2.printStackTrace();
				}
			}
		});
		deleteButton.setIcon(new ImageIcon(HeadQuarterMainWindow.class.getResource("/resources/delete.png")));
		headerButtonPanel.add(deleteButton);
		
		//Search Button
		JButton searchButton = new JButton("");
		searchButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SearchMap frame = new SearchMap();
				frame.setVisible(true);
			}
		});
		searchButton.setIcon(new ImageIcon(HeadQuarterMainWindow.class.getResource("/resources/search.png")));
		headerButtonPanel.add(searchButton);
		
		//Print Button
		JButton printButton = new JButton("");
		printButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, "Print Service is not available !!", "alert", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		printButton.setIcon(new ImageIcon(HeadQuarterMainWindow.class.getResource("/resources/print.png")));
		headerButtonPanel.add(printButton);
		
		//Refresh Button
		JButton refreshButton = new JButton("");
		refreshButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					updateMapTable();
				} catch (ClassNotFoundException e1) {
					JOptionPane.showMessageDialog(null, "Error Connecting Database!!", "alert", JOptionPane.INFORMATION_MESSAGE);
					e1.printStackTrace();
				}
			}
		});
		refreshButton.setIcon(new ImageIcon(HeadQuarterMainWindow.class.getResource("/resources/refresh.png")));
		headerButtonPanel.add(refreshButton);

		JPanel panel = new JPanel();
		splitPane.setLeftComponent(panel);
		panel.setLayout(new GridLayout(20, 1, 0, 0));
		productButton = new JButton("Product");
		productButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				headerLabel.setText(productButton.getText());
			}
		});
		panel.add(productButton);

		vendorsButton = new JButton("Vendors");
		vendorsButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				headerLabel.setText(vendorsButton.getText());
//				try {
//					updateTable("Vendors");
//				} catch (ClassNotFoundException e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
			}
		});
		panel.add(vendorsButton);

		purchaseOrderButton = new JButton("Purchase Order");
		purchaseOrderButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				headerLabel.setText(purchaseOrderButton.getText());
			}
		});
		panel.add(purchaseOrderButton);

		receivingButton = new JButton("Receiving");
		receivingButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				headerLabel.setText(receivingButton.getText());
			}
		});
		panel.add(receivingButton);

		paymentToVendorsButton = new JButton("Payment to Vendors");
		paymentToVendorsButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				headerLabel.setText(paymentToVendorsButton.getText());
			}
		});
		panel.add(paymentToVendorsButton);

		customersButton = new JButton("Customers");
		customersButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				headerLabel.setText(customersButton.getText());
			}
		});
		panel.add(customersButton);

		salesOrderButton = new JButton("Sales Order");
		salesOrderButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				headerLabel.setText(salesOrderButton.getText());
			}
		});
		panel.add(salesOrderButton);

		invoiceButton = new JButton("Invoices");
		invoiceButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				headerLabel.setText(invoiceButton.getText());
			}
		});
		panel.add(invoiceButton);

		customersPaymentsButton = new JButton("Customers Payments");
		customersPaymentsButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				headerLabel.setText(customersPaymentsButton.getText());
			}
		});
		panel.add(customersPaymentsButton);
	}
	
	static public void updateMapTable() throws ClassNotFoundException{
		db.ConnectApplicationDB();
		table.setModel(db.readData("map"));
		table.repaint();
		db.closeDB();
	}
	
	static public void updateTable(String tableName) throws ClassNotFoundException{
		db.ConnectApplicationDB();
		table.setModel(db.readData(tableName));
		table.repaint();
		db.closeDB();
	}
	
	static public void updateMapTableOnCondition(String tableName,String whereClause) throws ClassNotFoundException, TableEmptyException{
		db.ConnectApplicationDB();
		table.clearSelection();
		table.setModel(db.readDataOnCondition(tableName, whereClause));
		table.repaint();
		db.closeDB();
		if(table.getRowCount()==0){
			throw new TableEmptyException();
		}
	}

	public JFrame getFrame() {
		return headQuarterFrame;
	}

}
